home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-launchpad-bugs / examples / move_duplicates.py < prev    next >
Encoding:
Python Source  |  2009-02-27  |  2.1 KB  |  62 lines

  1. #!/usr/bin/python
  2.  
  3. cookie="/home/k/.bughelper/lpcookie.txt"
  4.  
  5. import launchpadbugs.connector as Connector
  6. import sys
  7.  
  8. from optparse import OptionParser
  9. parser = OptionParser()
  10. parser.add_option("--also-move-masterbugs", dest="move_masterbugs",
  11.     help="Also move the previous masterbugs of the given bugs. "
  12.         "Usually results in more bugs being touched.", action="store_true")
  13. parser.add_option("-m", "--new_masterbug", dest="masterbug", type="int")
  14. parser.set_usage("usage: %prog -m NEWMASTER [--also-move-masterbugs] bug1 bug2 ...")
  15.  
  16. (options, args) = parser.parse_args()
  17. if len(args) == 0:
  18.     parser.error("need bugs to process")
  19. if not options.masterbug:
  20.     parser.error("use -m for setting the new masterbug")
  21.  
  22.  
  23. def mass_change_duplicates(bugs, handled_list, new_dup, level=0, and_their_masters=False):
  24.     if level == 0:    # first check if the new masterbug is not a duplicate
  25.             # otherwise we can't assign duplicates to it
  26.         lp_masterbug = Bug(new_dup)
  27.         lp_masterbug.duplicate_of = None
  28.         lp_masterbug.commit()
  29.         handled_list.add(new_dup)
  30.     for b in bugs:
  31.         if b in handled_list or b == new_dup:
  32.             # either we processed this bug already or it is the new masterbug
  33.             continue
  34.         bug = Bug(b)
  35.         old_dup = bug.duplicate_of
  36.         if old_dup != new_dup:
  37.             print "%sHandling bug %d: %s" % ("\t"*level, b, bug.title)
  38.             handled_list.add(b) # don't process it again
  39.  
  40.             # now process all duplicates of this bug, minus already handled ones.
  41.             found = bug.duplicates - handled_list
  42.             if len(found) > 0:
  43.                 print "\t"*level, "-> Found:", ', '.join([ str(x) for x in found])
  44.                 mass_change_duplicates(found, handled_list, new_dup, level+1)
  45.  
  46.             print "\t"*level,"setting duplicate:", old_dup,"->", new_dup
  47.  
  48.             bug.duplicate_of = new_dup
  49.             bug.commit()
  50.  
  51.             if and_their_masters and old_dup and old_dup not in handled_list:
  52.                 print "\t"*level, "-> Found:", old_dup
  53.                 mass_change_duplicates(set([old_dup]), handled_list, new_dup, level+1)
  54.  
  55. Bug = Connector.ConnectBug()
  56. Bug.authentication=cookie
  57.  
  58. dup_list = set()
  59. start_list = set([ int(x) for x in args ])
  60.  
  61. mass_change_duplicates(start_list, dup_list, options.masterbug, and_their_masters=options.move_masterbugs)
  62.